#include "StdAfx.h"
#include "SMS_Writer.h"
#include "SMS_Command.h"
#include "../SerialLibrary/SerialMFC.h"
#include "../WT1011.h"
#include <string>

CSMS_Writer::CSMS_Writer(void)
:pfactory(new CSMS_CommandFactory())
{
}

CSMS_Writer::~CSMS_Writer(void)
{
	delete pfactory;
}


void CSMS_Writer::SendCommand_SetTextMode( void )
{
	std::string str_textmode=pfactory->CreateCommand_TextMode();
	SerialWriter(str_textmode);
	

	std::string str_oa=pfactory->CreateCommand_OA();
	SerialWriter(str_oa);

}

void CSMS_Writer::SendCommand_NewContent( std::string& var_number,std::string& var_content )
{
	std::string str_before=pfactory->CreateCommand_WriteNewSMS_Before(var_number);
	SerialWriter(str_before);

	

	std::string str_oa("\x0D");  //=pfactory->CreateCommand_OA();
	SerialWriter(str_oa);
	Sleep(500);
	//std::string str_sms=pfactory->CreateCommand_WriteNewSMS_Content(var_content);
	//SerialWriter(str_sms);
	
	SerialWriter(var_content);
	Sleep(100);

	std::string str_after("\x1A");//=pfactory->CreateCommand_WriteNewSMS_After();
	SerialWriter(str_after);

}

void CSMS_Writer::SendCommand_GetUnReadSMSContent( void )
{
	std::string str_before=pfactory->CreateCommand_ReadNewSMS();
	SerialWriter(str_before);

	std::string str_oa=pfactory->CreateCommand_OA();
	SerialWriter(str_oa);	
}

void CSMS_Writer::SendCommand_DelReadedSMSContent( void )
{
	std::string str_delreaded=pfactory->CreateCommand_DeleteReadedSMS();
	SerialWriter(str_delreaded);

	std::string str_oa=pfactory->CreateCommand_OA();
	SerialWriter(str_oa);
}

void CSMS_Writer::SerialWriter( std::string & var )
{
	Sleep(100);
	::SendMessage(theApp.hwnd_view,WM_VIEW_SMS_SENDDATA,WPARAM(var.c_str()),LPARAM(&var));
}

void CSMS_Writer::SendCommand_SetOutputAlarm( std::string& var_number,int var_input,bool var_onoff )
{
	std::string str_content("*RLY");
	str_content.append(boost::lexical_cast<std::string>(var_input));
	str_content.append("#");
	
	if (var_onoff) 
	{
		str_content.append("ON");
	}
	else
	{
		 str_content.append("OFF");
	}

	SendCommand_NewContent(var_number,str_content);

}